//
//  QLWebViewController.m
//  jSCoreTest
//
//  Created by rain on 16/8/12.
//  Copyright © 2016年 company. All rights reserved.
//

#import "RainWebViewController.h"
#import "UIWebView+JSCategory.h"
#import "RainWebViewJSObject.h"

@interface RainWebViewController ()<RainWebViewDelegate>

@end

@implementation RainWebViewController
@synthesize webView = _webView;

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




#pragma mark-- RainWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [self.webView.context evaluateScript:@"ider.zheng = 21"];//这里是检查报错的调用
//    [self.webView.context evaluateScript:@"jsFunction('native调用了js的函数')"];
    //或者
//    JSValue *jsFunctionValue = self.webView.context[@"jsFunction"];
//    [jsFunctionValue callWithArguments:@[@"native调用了js的函数"]];
    //
    
    //或者
    
    [self.webView stringByEvaluatingJavaScriptFromString:@"jsFunction('native调用了js的函数')"];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    
}

/*! @brief 获取当前的JSContext */
- (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext *)context {
    RainWebViewJSObject *webJSobject = [RainWebViewJSObject shareInstance];
    [webJSobject registerExceptionHandlerForContext:context];
    [webJSobject registerObjectNameForContext:context];
    
}

/*! @brief 获取网页自带Head Title */
- (void)webView:(UIWebView *)webView didReceiveTitle:(NSString *)title {
    
}

/*! @brief 只带“确定”按钮的告警弹框 */
- (void)webView:(UIWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message {
    
}

/*! @brief 带“确定”和“取消”按钮的告警弹框 */
- (void)webView:(UIWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message {
    
}

#pragma mark-- getter && setter

- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _webView.delegate = self;
        [self.view addSubview:_webView];
    }
    return _webView;
    
}


- (void)setUrlStr:(NSString *)urlStr {
    _urlStr = urlStr;
    NSURL *currentUrl = [NSURL URLWithString:_urlStr];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:currentUrl];
    [self.webView loadRequest:urlRequest];
}

@end


